package com.bobo.mm;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.bobo.mm.socket.decoder.LineBasedFrameDecoder;

/**
 * socket 连接的基本信息配置
 * Created by cocolove2 on 2018/5/29.
 */
public class ClientConfig implements Parcelable {
    /**
     * 自定义通道唯一编码(多客户端时根据该编码进行分发)
     */
    private String clientId;
    private String socketUrl;
    private String websocketUrl;
    /**
     * 是否自动重连
     */
    private boolean isAutoReconn = true;
    /**
     * 重连自增步长(10s)
     */
    private int reconnectInterval = 10 * 1000;
    /**
     * 最大重连间隔(2min)
     */
    private int reconnectMaxTime = 120 * 1000;
    /**
     * socket框架类型
     * {@link com.bobo.mm.SocketFrameworkType}
     */
    private String frameworkType;

    /**
     * 解码器处理的最大长度，默认1024
     */
    private int decodeMaxLength = 1024;
    /**
     * 数据解码器配置，默认使用LineBasedFrameDecoder
     */
    private String decodeClsName = LineBasedFrameDecoder.class.getName();


    public String getSocketUrl() {
        return socketUrl;
    }

    public void setSocketUrl(String socketUrl) {
        this.socketUrl = socketUrl;
    }

    public String getWebsocketUrl() {
        return websocketUrl;
    }

    public void setWebsocketUrl(String websocketUrl) {
        this.websocketUrl = websocketUrl;
    }

    public boolean isAutoReconn() {
        return isAutoReconn;
    }

    public void setAutoReconn(boolean autoReconn) {
        isAutoReconn = autoReconn;
    }

    public int getReconnectInterval() {
        return reconnectInterval;
    }

    public void setReconnectInterval(int reconnectInterval) {
        this.reconnectInterval = reconnectInterval;
    }

    public int getReconnectMaxTime() {
        return reconnectMaxTime;
    }

    public void setReconnectMaxTime(int reconnectMaxTime) {
        this.reconnectMaxTime = reconnectMaxTime;
    }


    public int getDecodeMaxLength() {
        return decodeMaxLength;
    }

    public void setDecodeMaxLength(int decodeMaxLength) {
        this.decodeMaxLength = decodeMaxLength;
    }

    public String getDecodeClsName() {
        return decodeClsName;
    }

    public void setDecodeClsName(String decodeClsName) {
        this.decodeClsName = decodeClsName;
    }

    public String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public String getFrameworkType() {
        if (frameworkType == null) {
            frameworkType = SocketFrameworkType.SOCKET_JAVA_BLOCK;
        }
        return frameworkType;
    }

    public void setFrameworkType(String frameworkType) {
        this.frameworkType = frameworkType;
    }

    @Override
    public int hashCode() {
        return clientId.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof ClientConfig) {
            return this.clientId.equals(((ClientConfig) obj).clientId);
        }
        return super.equals(obj);
    }

    /**
     * 基本配置是否有效
     *
     * @return true 有效
     */
    public boolean isValid() {
        String[] type = getFrameworkType().split(":");
        if ("socket".equals(type[0])) {
            return !TextUtils.isEmpty(this.socketUrl) && this.socketUrl.contains(":");
        } else {
            return !TextUtils.isEmpty(this.websocketUrl);
        }
    }


    public ClientConfig(String channelId) {
        this.clientId = channelId;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.clientId);
        dest.writeString(this.socketUrl);
        dest.writeString(this.websocketUrl);
        dest.writeByte(this.isAutoReconn ? (byte) 1 : (byte) 0);
        dest.writeInt(this.reconnectInterval);
        dest.writeInt(this.reconnectMaxTime);
        dest.writeString(this.frameworkType);
        dest.writeInt(this.decodeMaxLength);
        dest.writeString(this.decodeClsName);
    }

    protected ClientConfig(Parcel in) {
        this.clientId = in.readString();
        this.socketUrl = in.readString();
        this.websocketUrl = in.readString();
        this.isAutoReconn = in.readByte() != 0;
        this.reconnectInterval = in.readInt();
        this.reconnectMaxTime = in.readInt();
        this.frameworkType = in.readString();
        this.decodeMaxLength = in.readInt();
        this.decodeClsName = in.readString();
    }

    public static final Creator<ClientConfig> CREATOR = new Creator<ClientConfig>() {
        @Override
        public ClientConfig createFromParcel(Parcel source) {
            return new ClientConfig(source);
        }

        @Override
        public ClientConfig[] newArray(int size) {
            return new ClientConfig[size];
        }
    };
}
